"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
var cacheWithClean = exports.cacheWithClean = function cacheWithClean(fn) {
    var _this = this;

    var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
        _ref$maxAge = _ref.maxAge,
        maxAge = _ref$maxAge === undefined ? null : _ref$maxAge;

    var lastTime = 0;
    var rst = void 0;
    var clean = function clean() {
        if (maxAge) {
            lastTime = 0;
        }
        rst = undefined;
    };
    var cached = function cached() {
        if (!maxAge) {
            if (rst === undefined) {
                rst = fn.call(_this);
            }
            return rst;
        }

        var now = new Date().getTime();
        if (now - lastTime > maxAge) {
            rst = fn.call(_this);
            lastTime = now;
        }
        return rst;
    };
    return {
        cached: cached,
        clean: clean
    };
};
var cache = exports.cache = function cache(fn) {
    var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
        _ref2$maxAge = _ref2.maxAge,
        maxAge = _ref2$maxAge === undefined ? null : _ref2$maxAge;

    return cacheWithClean(fn, {
        maxAge: maxAge
    }).cached;
};
exports.default = cache;